package cn.dev123.util.tools;


import java.util.Calendar;
import java.util.Formatter;

/**
 * @author hunter
 * 
 */
public class HighFormatter {
	public final static char[] DIGITS = {
		    '0' , '1' , '2' , '3' , '4' , '5' ,
			    '6' , '7' , '8' , '9' , 'a' , 'b' ,
			    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
			    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
			    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
			    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
	};
	final static char[] DIGITS_CASE= {
	    '0' , '1' , '2' , '3' , '4' , '5' ,
		    '6' , '7' , '8' , '9' , 'A' , 'B' ,
		    'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
		    'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
		    'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
		    'U' , 'V' , 'W' , 'X' , 'Y' , 'Z'
};
	public final static String YEAR="%1$tY";
	public final static String MONTH="%1$tm";
	public final static String DAY="%1$te";
	public final static String HOUR="%1$tI";
	public final static String MINUTE="%1$tM";
	public final static String SECOND="%1$tS";
    public HighFormatter(){}
    
    /**
     * 
     * @param integer
     * @param isMemoryView  16----0x,8 ----0
     * @param isCase   true--A ,false--a
     * @return
     */
    public static String integerToHexString(int integer,boolean isMemoryView,boolean isCase){
    	  return toUnsignedString(integer,4,isMemoryView,isCase);
    }
    
    /**
     * 
     * @param c
     * @param isMemoryView  16----0x,8 ----0
     * @param isCase   true--A ,false--a
     * @return
     */
    public static String integerToHexString(char c,boolean isMemoryView,boolean isCase){
  	  return toUnsignedString(c,4,isMemoryView,isCase);
  }
    
    /**
     * 
     * @param integer
     * @param isMemoryView  16----0x,8 ----0 
     * @return
     */
    public static String integerToBinaryString(int integer,boolean isMemoryView){
    	return toUnsignedString(integer,1,isMemoryView,false);
    }
    
    
    /**
     * 
     * @param integer
     * @param isMemoryView  16----0x,8 ----0 
     * @return
     */
    public static String integerToOctalString(int integer,boolean isMemoryView){
    	return toUnsignedString(integer,3,isMemoryView,false);
    }
    /**
     * 
     * @param integer
     * @return
     */
    public static String integerToDecimalString(int integer){
    	return integer+"";
    }
    
    
    /**
     * 
     * @param i
     * @param shift     16---4,8---3,2-----1
     * @param isMemoryView  16----0x123456789ABCDEF ,8-----01234567,10----1234567890 ,2----1010101
     * @return
     */
	public static String toUnsignedString(int i, int shift,boolean isMemoryView,boolean isCase) {
		char[] buf = new char[32];		
		int charPos = 32;
		int radix = 1 << shift;
		int mask = radix - 1;
		do {
			buf[--charPos] = isCase?DIGITS_CASE[i & mask]:DIGITS[i & mask];
			i >>>= shift;
		} while (i != 0);
		if(isMemoryView){
			if(shift==3){
				buf[--charPos]='0';
			}else  if(shift==4){
		         buf[--charPos]='x';buf[--charPos]='0';
			}
		}
		return new String(buf, charPos, (32 - charPos));
	}
	
	/**
	 * 根據格式獲取當前時間
	 * @param framt
	 * @return
	 */
	public static String toStringByCurrentTime(String framt){
		Calendar  calendar=Calendar.getInstance();
		Formatter formatter=new Formatter();
		return formatter.format(framt,calendar).toString();
	}
	

    
	/**
	 * 全角轉換為半角
	 * @param input
	 * @return
	 */
	public static String ToDBC(String input)
	{
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++)
		{
			if (c[i] == 12288)
			{
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
						c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}
	
    /**
     * 16進制字符串轉換成普通字符串
     * @param hexString
     * @return
     */
	public static String toStringByHexString(String hexString) {
    	StringBuffer temp=new StringBuffer();
    	String temps[]=hexString.split("0x");
    	for (int i = 1; i < temps.length; i++) {
    		temp.append(String.format("%c", Integer.parseInt(temps[i],16)));
		}
    	return temp.toString();
    }
    /**
     * 普通字符串轉換還成16進制字符串
     * @param tagString
     * @return
     */
	public static String toHexStringByString(String tagString) {
    	StringBuffer temp=new StringBuffer();
		char[] chars=tagString.toCharArray();
    	for (int i = 0; i < chars.length; i++) {
			   temp.append(integerToHexString(chars[i], false, true));
		}
    	return temp.toString();
    }

	public static void main(String[] args) {
		
	}
}